package com.tqjc.system.core.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 86195
 * @CreateTime: 2025-04-25  15:09
 * @Description: TODO
 * @Version: 1.0
 */
@Configuration
public class DruidConfig {

    /**
     * 返回后台监控servlet
     */
    @Bean
    public ServletRegistrationBean monitor(){
        //请求
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        //后台需要用户登录，账号密码配置
        HashMap<String, String> initParameters = new HashMap<>();
        //增加配置
        //登录key是固定的loginUsername loginPassword
        //TODO 这里改成你需要的用户名和密码
        initParameters.put("loginUsername","admin");
        initParameters.put("loginPassword","admin");
        //访问白名单
//        initParameters.put("allow","ip地址");
        bean.setInitParameters(initParameters);//设置初始化参数
        return bean;
    }

    /**
     * filter
     * @return
     */
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean<Filter> filterBean = new FilterRegistrationBean<>();
        filterBean.setFilter(new WebStatFilter());
        //可以过滤那些请求
        Map<String, String> initParameters = new HashMap<>();
        //不统计这些东西
        initParameters.put("exclusions","*.js,*.css,/druid/*");
        filterBean.setInitParameters(initParameters);
        return filterBean;
    }

}
